﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Newton
{
    class Player : GameObject
    {
        #region "atributos"

        int points;

        int lifes;
        
        public int playerId = 0;

        public int angulo;

        public Direction actual_direction = Direction.RIGHT;

        public Tiro tiro;

        public ContentManager content;

        //animation parado;
        //animation atirando;
        //animation cima1;
        //animation cima2;

        public Animation baixo;
        public Animation atirandoBaixo;
        public Animation medio;
        public Animation atirandoMedio;
        public Animation alto;
        public Animation atirandoAlto;

        
        public Rectangle CollisionRect
        {
            get
            {
                return new Rectangle((int)position.X, (int)position.Y, currentAnimation.frameWidth, currentAnimation.frameHeight);
            }
            set
            {
                base.CollisionRect = value;
            }
        }

        #endregion

        #region "metodos"

        

        public Player(GameWindow Window, Texture2D Texture, ContentManager Content)
        {
            this.Texture = Texture;
            this.Window = Window;
            this.content = Content;

            this.Velocity = new Vector2(4, 4);
            this.Position = new Vector2(Window.ClientBounds.Width / 2, Window.ClientBounds.Height / 2);
            this.points = 0;
            this.lifes = Game1.QTD_INICIAL_VIDAS;

            //baixo = new Animation("baixo", 60, 60, 0, 0, 1, 1, true);
            //medio = new Animation("medio", 60, 60, 60, 0, 1, 1, true);
            //alto = new Animation("alto", 60, 60, 120, 0, 1, 1, true);
            //atirandoBaixo = new Animation("atirandoBaixo", 65 , 60, 0, 60, 5, 3, true);
            //atirandoMedio = new Animation("atirandoMedio", 55, 60, 60, 120, 4, 3, true);
            //atirandoAlto = new Animation("atirandoAlto", 60, 60, 60, 180, 4, 3, true);

            baixo = new Animation("baixo", 60, 60, 0, 0, 1, 1, true);
            medio = new Animation("medio", 60, 60, 60, 0, 1, 1, true);
            alto = new Animation("alto", 60, 60, 120, 0, 1, 1, true);
            atirandoBaixo = new Animation("atirandoBaixo", 60, 60, 0, 55, 5, 2, true);
            atirandoMedio = new Animation("atirandoMedio", 60, 60, 60, 110, 4, 2, true);
            atirandoAlto = new Animation("atirandoAlto", 60, 60, 60, 170, 4, 2, true);

            //parado = new animation();
            //parado.frameCount = 1;
            //parado.frameHeight = 60;
            //parado.frameRate = 1;
            //parado.frameWidth = 60;
            //parado.mapX = 0;
            //parado.mapY = 0;

            //atirando = new animation();
            //atirando.frameCount = 3;
            //atirando.frameHeight = 30;
            //atirando.frameRate = 1;
            //atirando.frameWidth = 30;
            //atirando.mapX = 120;
            //atirando.mapY = 0;

            //cima1 = new animation();
            //cima1.frameCount = 2;
            //cima1.frameHeight = 60;
            //cima1.frameRate = 1;
            //cima1.frameWidth = 60;
            //cima1.mapX = 60;
            //cima1.mapY = 0;

            //cima2 = new animation();
            //cima2.frameCount = 3;
            //cima2.frameHeight = 60;
            //cima2.frameRate = 1;
            //cima2.frameWidth = 60;
            //cima2.mapX = 120;
            //cima2.mapY = 0;    

            currentAnimation = alto;
           
        }

        public override void Update()
        {

        }

        public void Update(GameTime gameTime, KeyboardState keyboardState)
        {
        
            if (playerId == 1)
            {
                if (!currentAnimation.IsPlaying())
                {
                    if (this.angulo < 45)
                    {
                        this.SetOffset(0, 0);
                        this.currentAnimation = this.baixo;
                        this.currentAnimation.GoToAndStop(0);
                    }
                    else if (this.angulo >= 45 && this.angulo <= 60)
                    {
                        //this.SetOffset(-7, 1);
                        this.SetOffset(0, 0);
                        this.currentAnimation = this.medio;
                        this.currentAnimation.GoToAndStop(1);
                    }
                    else
                    {
                        //this.SetOffset(-4, 1);
                        this.SetOffset(0, 0);
                        this.currentAnimation = this.alto;
                        this.currentAnimation.GoToAndStop(2);
                    }
                }

                if (Game1.keyboardState.IsKeyDown(Keys.Space) &&
                    !Game1.oldKeyboardState.IsKeyDown(Keys.Space))
                {
                    tiro = new Tiro(Window, this.content);
                    tiro.Position = new Vector2(this.Position.X, this.Position.Y);
                    tiro.Velocity = new Vector2(1, 1);
                

                    if (this.angulo < 45)
                    {
                        //this.SetOffset(-5, 8);
                        this.SetOffset(0, -4);
                        currentAnimation = atirandoBaixo;
                        currentAnimation.PlayFromInit();
                        
                    }
                    else if (this.angulo >= 45 &&
                            this.angulo < 60)
                    {
                        //this.SetOffset(-5, 8);
                        this.SetOffset(0, -9);
                        currentAnimation = atirandoMedio;
                        currentAnimation.PlayFromInit();
                        
                    } else if (this.angulo > 60)
                    {
                        //this.SetOffset(-5, 8);
                        this.SetOffset(0, -9);
                        currentAnimation = atirandoAlto;
                        currentAnimation.PlayFromInit();
                    }

                }

                if (Game1.keyboardState.IsKeyDown(Keys.Right) &&
                   !Game1.oldKeyboardState.IsKeyDown(Keys.Right))
                {
                    if (actual_direction != Direction.RIGHT)
                    {
                        // Aramaki
                        actual_direction = Direction.RIGHT;
                        //currentAnimation = baixo;
                        //currentAnimation.GoToAndStop(0);
                    }

                }
                else
                    if (Game1.keyboardState.IsKeyDown(Keys.Left) &&
                            !Game1.oldKeyboardState.IsKeyDown(Keys.Left))
                    {
                        if (actual_direction != Direction.LEFT)
                        {
                            // Aramaki
                            actual_direction = Direction.LEFT;
                            //currentAnimation = baixo;
                            //currentAnimation.GoToAndStop(0);
                        }

                    }
                    else
                        if (Game1.keyboardState.IsKeyDown(Keys.Down) &&
                            !Game1.oldKeyboardState.IsKeyDown(Keys.Down))
                        {
                            if (actual_direction != Direction.DOWN)
                            {
                                // Aramaki
                                actual_direction = Direction.DOWN;
                                //currentAnimation = baixo;
                                //currentAnimation.GoToAndStop(0);
                            }
                        }
                        else
                            if (Game1.keyboardState.IsKeyDown(Keys.Up) &&
                            !Game1.oldKeyboardState.IsKeyDown(Keys.Up))
                            {
                                if (actual_direction != Direction.UP)
                                {
                                    // Aramaki
                                    actual_direction = Direction.UP;
                                    //currentAnimation = baixo;
                                    //currentAnimation.GoToAndStop(0);

                                }
                            }
            }

            switch (actual_direction)
            {
                case Direction.LEFT:
                    position.X -= Velocity.X;
                    actual_direction = Direction.STOPPED;
                    break;
                case Direction.RIGHT:
                    position.X += Velocity.X;
                    actual_direction = Direction.STOPPED;
                    break;
                case Direction.UP:
                    position.Y -= Velocity.Y;
                    actual_direction = Direction.STOPPED;
                    break;
                case Direction.DOWN:
                    position.Y += Velocity.Y;
                    actual_direction = Direction.STOPPED;
                    break;
                
            }

            CollisionRect = new Rectangle((int)position.X, (int)position.Y, currentAnimation.frameWidth, currentAnimation.frameHeight);

        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (this.playerId == 1)
            {
                switch (actual_direction)
                {
                    case Direction.LEFT:
                        base.Draw(gameTime, spriteBatch, SpriteEffects.FlipHorizontally);
                        //base.Draw(gameTime, spriteBatch, SpriteEffects.FlipHorizontally, 0, Vector2.Zero, currentAnimation.currentFrame);
                        break;
                    case Direction.RIGHT:
                        base.Draw(gameTime, spriteBatch, SpriteEffects.FlipHorizontally);
                        //base.Draw(gameTime, spriteBatch, SpriteEffects.FlipHorizontally, 0, Vector2.Zero, currentAnimation.currentFrame);
                        break;
                    case Direction.UP:
                        base.Draw(gameTime, spriteBatch,  SpriteEffects.FlipHorizontally);
                        //base.Draw(gameTime, spriteBatch, SpriteEffects.FlipHorizontally, 0, Vector2.Zero, currentAnimation.currentFrame);
                        break;
                    case Direction.DOWN:
                        base.Draw(gameTime, spriteBatch,  SpriteEffects.FlipHorizontally);
                        //base.Draw(gameTime, spriteBatch, SpriteEffects.FlipHorizontally, 0, Vector2.Zero, currentAnimation.currentFrame);
                        break;
                    default:
                        base.Draw(gameTime, spriteBatch, SpriteEffects.FlipHorizontally);
                        break;
                }
            }
            else if (this.playerId == 2)
            {
                switch (actual_direction)
                {
                    case Direction.LEFT:
                        base.Draw(gameTime, spriteBatch,  SpriteEffects.None);
                        //base.Draw(gameTime, spriteBatch, currentAnimation.currentFrame);
                        break;
                    case Direction.RIGHT:
                        //base.Draw(gameTime, spriteBatch);
                        base.Draw(gameTime, spriteBatch, SpriteEffects.None);
                        break;
                    case Direction.UP:
                        //base.Draw(gameTime, spriteBatch);
                        base.Draw(gameTime, spriteBatch, SpriteEffects.None);
                        break;
                    case Direction.DOWN:
                        //base.Draw(gameTime, spriteBatch, 3);
                        base.Draw(gameTime, spriteBatch, SpriteEffects.None);
                        break;
                    default:
                        base.Draw(gameTime, spriteBatch, SpriteEffects.None);
                        break;
                }
            }
        }

        #endregion "metodos"
    }
}
